package com.scm.service.impl;

import com.google.gson.Gson;
import com.scm.common.core.domain.entity.SysUser;
import com.scm.persist.model.ScmOrder;
import com.scm.persist.model.ScmOrderProduct;
import com.scm.persist.model.ScmOrderProductExample;
import com.scm.persist.mybatis.ScmOrderMapper;
import com.scm.persist.mybatis.ScmOrderProductMapper;
import com.scm.service.IScmOrderProductService;
import com.scm.service.IScmOrderService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * 订单主Service业务层处理
 * 
 * @author reptilemanager
 * @date 2024-02-08
 */
@Slf4j
@Service
public class ScmOrderProductServiceImpl implements IScmOrderProductService
{

    @Autowired
    ScmOrderProductMapper orderProductMapper;

    @Override
    public int saveOrUpdateScmOrderProduct(SysUser sysUser,long scmOrderId, List<ScmOrderProduct> list) {

        ScmOrderProductExample example = new ScmOrderProductExample();
        example.createCriteria().andScmOrderIdEqualTo(scmOrderId);
        int count = orderProductMapper.countByExample(example);
        if(count==0){
            insertList(scmOrderId,list);
        }else{
            orderProductMapper.deleteByExample(example);
            insertList(scmOrderId,list);
        }

        return 1;

    }

    @Override
    public List<ScmOrderProduct> selectScmOrderProductByOrderId(Long orderId) {

        ScmOrderProductExample example = new ScmOrderProductExample();
        example.createCriteria().andScmOrderIdEqualTo(orderId);
        List<ScmOrderProduct> list = orderProductMapper.selectByExample(example);
        return list;

    }


    private void insertList(long scmOrderId,List<ScmOrderProduct> list){

        for (ScmOrderProduct scmOrderProduct : list) {
            scmOrderProduct.setScmOrderId(scmOrderId);
            orderProductMapper.insertSelective(scmOrderProduct);
        }

    }

    public int updateByPrimaryKey(ScmOrderProduct scmOrderProduct){

        return orderProductMapper.updateByPrimaryKey(scmOrderProduct);

    }



    private void getproductByOfficemate( String productCode){

        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");

            RequestBody body = RequestBody.create(mediaType, "{\"page\":1,\"pageSize\":100,\"search\":{\"code\":\""+productCode+"\",\"brandNameList\":[]},\"ofs_page_list\":1}");
            Request request = new Request.Builder()
                    .url("https://scs.officemate.cn/supplier/product/productSap/supplierProductSapList")
                    .method("POST", body)
                    .addHeader("authority", "scs.officemate.cn")
                    .addHeader("accept", "application/json, text/plain, */*")
                    .addHeader("accept-language", "zh-CN,zh;q=0.9")
                    .addHeader("appsecret", "hxBNGXUE22eXp4td01XSiUhUYPosQg7149646l3J6n5XHf8Qe6T2E868841HgB56")
                    .addHeader("authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvc2NzLm9mZmljZW1hdGUuY25cL29wZW5cL2F1dGhcL3N1cHBsaWVyTG9naW5CeUxpZm5yIiwiaWF0IjoxNzA2OTc0NTY3LCJleHAiOjE3MDc1NzkzNjcsIm5iZiI6MTcwNjk3NDU2NywianRpIjoiUE01OUpPR3dwU0VUTW5TRSIsInN1YiI6NDUyMjEsInBydiI6IjY2NDgyYjZkNjMzOTdiYjkyZjQ3NzQwNDMyN2RlNTE5ZGYzZmNlZmEiLCJwbGF0Zm9ybSI6Im9zcCJ9.7lzZ1NpQRqgH9JTLnlEjWh8IMQfx5jT4bp1kinBjdDA")
                    .addHeader("content-type", "application/json")
                    .addHeader("origin", "https://osp.officemate.cn")
                    .addHeader("referer", "https://osp.officemate.cn/")
                    .addHeader("sec-ch-ua", "\".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"")
                    .addHeader("sec-ch-ua-mobile", "?0")
                    .addHeader("sec-ch-ua-platform", "\"macOS\"")
                    .addHeader("sec-fetch-dest", "empty")
                    .addHeader("sec-fetch-mode", "cors")
                    .addHeader("sec-fetch-site", "same-site")
                    .addHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36")
                    .addHeader("usercode", "6C48A9-1374EF-EF2F6A-7DDBA1")
                    .addHeader("Cookie", "acw_tc=0a5cc90217070110357671892ea33ae13970b79811a99e0ac668f7da4b7fad; laravel_session=KMDRDgXc2fUgtxU8qQu1xyjFMSbJId5wHRcHXql9")
                    .build();
            Response response = client.newCall(request).execute();
            ResponseBody body2 = response.body();
            String productBody =  IOUtils.toString(body2.bytes(),"utf-8");
            String d = StringEscapeUtils.unescapeJava(productBody);

            OmsProductResp resp = new Gson().fromJson(d,OmsProductResp.class);
            log.info("response:{},{}",response.message(), d);
            System.out.println("end");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {

        ScmOrderProductServiceImpl obj = new ScmOrderProductServiceImpl();
        obj.getproductByOfficemate("2198357");
    }
}
